package org.imageMaker.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.imageMaker.utils.Coords;
import org.imageMaker.utils.Parser;
import org.xml.sax.SAXException;

public class ZipUtils {
	
	private static Map<String,Coords> coordenadas;
	private static ImageIO imagen;
	
	public static void unzip(String path, String folderName) throws IOException{
		File f = new File(path);
		  ZipFile zip;
		  zip = new ZipFile(f);
		  Enumeration e = zip.entries();
		  while( e.hasMoreElements() ) {
		   ZipEntry zen = (ZipEntry) e.nextElement();
		   if (zen.isDirectory()){
		    continue;
		   }
		   int size =(int) zen.getSize();
		   InputStream zis = zip.getInputStream(zen);

		   File dest = new File(f.getParentFile().getAbsolutePath() + File.separator + folderName);
		   if (!dest.exists()){
			   dest.mkdir();
		   }
		   String extractfile = f.getParentFile().getAbsolutePath() + File.separator + folderName + File.separator + zen.getName();
		   
		   writeFile(zis, new File(extractfile),size);
		   zis.close();
		  }
		  zip.close();  
		 }
	
	@SuppressWarnings({ "unused", "rawtypes" })
	public static void unzipWithoutWrite(String path) throws Exception{
		File f = new File(path);
		  ZipFile zip;
		  zip = new ZipFile(f);
		  Enumeration e = zip.entries();
		  while( e.hasMoreElements() ) {
		   ZipEntry zen = (ZipEntry) e.nextElement();
		   if (zen.isDirectory()){
		    continue;
		   }
		   int size =(int) zen.getSize();
		   InputStream zis = zip.getInputStream(zen);
		   if (zen.getName().equalsIgnoreCase("gpd.xml")){
			   //XML de coordenadas
			   coordenadas = Parser.start(zis);
		   }
		   
		   else if (zen.getName().equalsIgnoreCase("processimage.jpg")){
			   //JPG imagen
			   imagen = new ImageIO(zis);
		   }
		   zis.close();
		  }
		  zip.close();  
		 }
	
	/***
	  * Permite Escribir un archivo de un jar a disco
	  * @throws IOException
	  */
	 private static void writeFile(InputStream zis, File file,int size) throws IOException {
	  File parentFile = file.getParentFile();
	  if(!parentFile.exists()){
	   parentFile.mkdirs();// this is important
	  }

	  FileOutputStream fos = null;
	  try {
	   fos = new FileOutputStream(file);
	   byte[] byteStream = new byte[ (int) size];
	   int buf = -1;
	   int rb = 0;
	   while ( ( ( (int) size - rb) > 0)) {
	    buf = zis.read(byteStream, rb, (int) size - rb);

	    if (buf == -1) {
	     break;
	    }
	    rb += buf;
	   }
	   fos.write(byteStream);
	  }
	  catch(IOException e) {
	   throw new IOException("UNZIP_ERROR");
	  }finally {
	   if(fos != null){
	    fos.close();
	   }
	  }
	 }
	 


	public static Map<String, Coords> getCoordenadas() {
		return coordenadas;
	}

	public static void setCoordenadas(Map<String, Coords> coordenadas) {
		ZipUtils.coordenadas = coordenadas;
	}

	public static ImageIO getImagen() {
		return imagen;
	}

	public static void setImagen(ImageIO imagen) {
		ZipUtils.imagen = imagen;
	}
		 
	public static void main (String [] args) throws Exception{
		ZipUtils.unzipWithoutWrite("D:/alfa-ccp-retirada-proceso.par");
	}
}